# -*- coding: utf-8 -*-
#you can get $accountid from https://account.console.aliyun.com/#/secure
#you can get $accid and $acckey from https://ak-console.aliyun.com/#/accesskey
#you can generate $endpoint: http://$accountid.mns.cn-hangzhou.aliyuncs.com, eg. http://1234567890123456.mns.cn-hangzhou.aliyuncs.com
import sys
from mns.account import Account
from mns.topic import *


class NdsSms:
    def __init__(self, debug=False):
		ENDPOINT="http://1752668288983271.mns.cn-hangzhou.aliyuncs.com/"
		ACCESS_KEY_ID="LTAITCEDARIg6zZC"
		ACCESS_KEY_SECRET="VuldhpXzZV8O4J1nKSJnjDYRhgQBLI"
		topic_name = "sms.topic-cn-hangzhou"

		self.free_sign_name1 = "新维空间站"
		self.template_code1 = "SMS_94660048"

		self.free_sign_name2 = "新维空间站"
		self.template_code2 = "SMS_94630054"

		self.msg_body = "I am test message."

		my_account = Account(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET)

		self.my_topic = my_account.get_topic(topic_name)


    def get_sms(self, status, phone, params):
    	print status
    	print params
    	if status == 1:
    		print "=========status 1111111111111============"
        	direct_sms = DirectSMSInfo(free_sign_name=self.free_sign_name1, template_code=self.template_code1, single=False)
        elif status == 2:
        	print "=========status 2222222222222============"
        	direct_sms = DirectSMSInfo(free_sign_name=self.free_sign_name2, template_code=self.template_code2, single=False)
        else:
        	direct_sms = DirectSMSInfo(free_sign_name=self.free_sign_name, template_code=self.template_code, single=False)

        direct_sms.add_receiver(receiver=phone, params=params)

        msg = TopicMessage(self.msg_body, "msg_tag", None, direct_sms)
        return msg


    def set_sms(self, msg):
		try:
		    re_msg = self.my_topic.publish_message(msg)
		    print "Publish Message Succeed. MessageBody:%s MessageID:%s" % (self.msg_body, re_msg.message_id)
		except MNSExceptionBase,e:
		    if e.type == "TopicNotExist":
		        print "Topic not exist, please create it."
		        sys.exit(1)
		    print "Publish Message Fail. Exception:%s" % e


    def send_sms(self, status, phone, params):
    	msg = self.get_sms(status, phone, params)
    	self.set_sms(msg)


sms = NdsSms()
params1 = {"code": "639857"}
sms.send_sms(1, "13699118095", params1)


params2 = {"name": "YuWen", "time": "2017-09-08 17:00:00", "address": "BeiJinDaXue"}
sms.send_sms(2, "13699118095", params2)